import os
import sys
import shutil
import subprocess


def copy_and_del(folder_path):
    files = os.listdir(folder_path)
    folders = []
    for file in files:
        if file == 'x64' or file == 'x86':
            continue
        path = os.path.join(folder_path, file)
        if os.path.isdir(path):
            folders.append(path)
    for path in folders:
        for file in os.listdir(path):
            if '.dll' in file or '.exe' in file:
                file_path = os.path.join(path + '\\', file)
                copy_path = os.path.join(folder_path, file)
                shutil.copyfile(file_path, copy_path)
        shutil.rmtree(path)


def msbuild(base_path):
    subprocess.check_call('MSBuild {} /t:Rebuild /p:Configuration=Release'.format(base_path + '\\Net2.0\\UtilTools.csproj'))
    subprocess.check_call('MSBuild {} /t:Rebuild /p:Configuration=Release'.format(base_path + '\\Net4.0\\UtilTools.csproj'))
    subprocess.check_call('MSBuild {} /t:Rebuild /p:Configuration=Release'.format(base_path + '\\Net4.5\\UtilTools.csproj'))


def encry_dll(base_path):
    project_path = base_path + '\\Net2.0\\bin\\Release\\netstandard2.0\\UtilTools.dll'
    subprocess.check_call('Reactor -q -project UtilTools.csproj', cwd=project_path)
    copy_and_del(project_path.re)
    project_path = base_path + '\\Net4.0\bin\Release\\UtilTools.dll'
    subprocess.check_call('Reactor -q -project UtilTools.csproj', cwd=project_path)
    project_path = base_path + '\\Net4.5\bin\Release\\UtilTools.dll'
    subprocess.check_call('Reactor -q -project UtilTools.csproj', cwd=project_path)


def run():
    base_path = os.path.dirname(os.path.realpath(sys.argv[0]))
    print(base_path)
    # msbuild(base_path)
    encry_dll(base_path)

if __name__ == "__main__":
    run()
    pass